//@ts-nocheck
import { media } from '@kit.MediaKit';
import { audio } from '@kit.AudioKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo } from '@kit.CoreFileKit';

import ResourceManager from '@ohos.resourceManager';

let avPlayer: media.AVPlayer;

export class AudioControl {
  static async initAudioPlayer(params: string) {
    console.info('kkk initAudioPlayer ' + params);
    AudioControl.avPlayer = await media.createAVPlayer();
    AudioControl.setCallback();
    let fileDescriptor = await getContext().resourceManager.getRawFd(params);
    AudioControl.avPlayer.fdSrc = fileDescriptor;
    console.info('kkk initAudioPlayer ' + fileDescriptor.fd);
  }

  static setCallback() {
    AudioControl.avPlayer.on('stateChange', async (state: string, reason: media.StateChangeReason) => {
      switch (state) {
        case 'idle':
          console.info('kkk state idle called');
          AudioControl.avPlayer.release();
          break;
        case 'initialized':
          AudioControl.hasInit = true;
          AudioControl.avPlayer.prepare();
          break;
        case 'prepared':
          console.info('kkk audio play prepared');
          AudioControl.avPlayer.play();
          break;
        case 'playing':
          console.info('kkk audio play success');
          break;
        case 'paused':
          console.info('kkk audio paused success');
          break;
        case 'error':
          console.info('kkk playerState error called');
          break;
        default:
          break;
      }
    })
  }


  static playMusic(params: string) {
    console.info('kkk playMusic', params);
    AudioControl.initAudioPlayer(params);
  }

  static stop() {
    console.info('kkk stop');
    AudioControl.avPlayer.stop();
    AudioControl.avPlayer.reset();
  }
}